package com.fhz.day005.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.databinding.BindingAdapter;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.ViewModelStoreOwner;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.fhz.day005.R;
import com.fhz.day005.databinding.RvLayoutBinding;
import com.fhz.day005.entity.Food;
import com.fhz.day005.fragment.FoodViewModel;
import com.fhz.day005.fragment.FoodViewModelFactory;

import java.util.List;

/**
 * 时间:2022/5/23
 *
 * @author msfeng
 * 简述: TODO
 */
public class FoodAdapter extends RecyclerView.Adapter<FoodAdapter.ViewHolder> {

    List<Food.Data> list;

    private Context context;

    public FoodAdapter(List<Food.Data> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @NonNull
    @Override
    public FoodAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.rv_layout, null);
        RvLayoutBinding bind = DataBindingUtil.bind(inflate);
        ViewHolder viewHolder = new ViewHolder(bind);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull FoodAdapter.ViewHolder holder, int position) {
        holder.binding.setFood(list.get(position));
        holder.binding.executePendingBindings();

        holder.binding.headerImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                click.click(holder.getAdapterPosition());
            }
        });
    }



    public interface MyClick{
        void click(int i);
    }
    private MyClick click;

    public void setClick(MyClick click) {
        this.click = click;
    }

    @Override
    public int getItemCount() {
        return list.size();
    }


    public class ViewHolder extends RecyclerView.ViewHolder {

        RvLayoutBinding binding;
        public ViewHolder(@NonNull RvLayoutBinding itemView) {
            super(itemView.getRoot());
            binding =  itemView;
        }
    }

    @BindingAdapter("imageUrl")
    public static void setUrl(ImageView imageView, String url){
        Glide.with(imageView.getContext()).load(url).into(imageView);
    }
}
